<?php
defined('WX_ROOT') OR define('WX_ROOT', __DIR__.'/');

require_once (WX_ROOT.'WxBase.php');
/** 
 * 微信企业付款API
 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
 * @abstract 接口调用规则：
 * <p> 给同一个实名用户付款，单笔单日限额2W/2W
 * <p> 给同一个非实名用户付款，单笔单日限额2000/2000
 * <p> 一个商户同一日付款总额限额100W
 * <p> 单笔最小金额默认为1元
 * <p> 每个用户每天最多可付款10次，可以在商户平台--API安全进行设置
 * <p> 给同一个用户付款时间间隔不得低于15秒
 */
class WxTransfers extends WxBase {
	public function __construct($cfg=array()){
		$this->configGroup = isset ( $cfg ['group'] ) ? $cfg ['group'] : 'yx';
		$this->apiUrl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
		$this->needSslCret = TRUE;
	}
	/**
	 * 调用哪个接口支付
	 * @param string 取值如下:
	 * 		<p>WEB : 调用公众号付款接口
	 * 		<p>APP : 调用移动应用付款接口
	 */
	public function payFrom($group = 'yx'){
		$this->configGroup = $group;
	}
	
	
	
	/**
	 * 微信转账
	 * @abstract 设置更多参数,在调用此方法前调用setParams()方法
	 * @param 	string	 		$openid  			微信第三方ID
	 * @param 	int		 		$amount				付款金额，单位:分
	 * @param 	string	 		$partner_trade_no	唯一商户订单号(默认:采用时间戳+3位随机数)
	 * @param 	string	 		$desc				说明信息(默认:时时赚提现)
	 * @return 	WechatPayResult 成功返回:结果对象;失败返回:FALSE
	 */
	public function transfres($openid,$amount,$partner_trade_no,$desc=''){
// 		$CI = &get_instance();
		$this->params = array();
		
		$this->setParams('mch_appid',$this->config('appid'))
			 ->setParams('mchid',$this->config('mch_id'))
			 ->setParams('nonce_str',uniqid())
			 ->setParams('partner_trade_no',$partner_trade_no)
			 ->setParams('openid',$openid)
			 ->setParams('check_name','NO_CHECK')
			 ->setParams('amount',$amount)
			 ->setParams('desc',$desc)
			 ->setParams('spbill_create_ip',parent::getServerIP())
			 ;
		
// 		$this->setParams('nonce_str','5a33a71403558');
// 		$this->setParams('partner_trade_no','5a33a71403170');
		
		$sign = self::sign($this->params,$this->config('key'));
		$this->setParams('sign',$sign);
	  	$sendXML = self::toXmlString($this->params);
// 	  	debug('params',$this->params);
// 	  	$CI->log->put('transfresParams',$this->params)->info('transfresParams');
	  	
	  	
	  	$sslPemFile=$this->config('sslPemFile');
	  	$sslPemKeyFile=$this->config('sslPemKeyFile');
	  	
// 	  	var_dump($sslPemFile,$sslPemKeyFile,$this);die;
	  	
	  	
	  	if(!is_file($sslPemFile)){
	  		throw new Exception("证书文件不存在:[{$sslPemFile}]");
	  	}
	  	if(!is_file($sslPemKeyFile)){
	  		throw new Exception("证书文件不存在:[{$sslPemKeyFile}]");
	  	}
	  	
// 	  	pr($this);
// 	  	pr($sslPemFile,$sslPemKeyFile);
// 	  	die;
	  	$this->_requestXML = $sendXML;
	  	$response  = self::curlSendXml($this->apiUrl, $sendXML,$sslPemFile,$sslPemKeyFile);
	  	$this->_responseXML = $response;
// 	  	var_dump($this->params,$response);
	  	/*
	  	pr( "request\n",htmlentities(preg_replace('/(<\/.*?>)/i', "$1\n", $sendXML)));
	  	pr("response\n".htmlentities($response));
	  	*/
	 	
	  	return $this->parseResponseNew($response);
// 	  	if ( $this->parseResponse($response) ) {// 转账成功
// 	  		return $this->response;
// 	  	}else{// 转账失败
// 	  		return FALSE;
// 	  	}
	}
	private $_requestXML='';
	public function getRequestXML(){
		return $this->_requestXML;
	}
	private $_responseXML='';
	public function getResponseXML(){
		return $this->_responseXML;
	}
	
	public function getLog(){
		return implode("\n", array(
				'request',
				$this->getRequestXML(),
				'response',
				$this->getResponseXML()
		));
	}
	
// 	private function getCretFile(){
// 		$cret = array();
// 		switch ($this->cfgGroup) {
// 			case 'app':
// 				$cret['sslPemFile'] = DATA_PATH.'wechat/app_cret/apiclient_cert.pem';
// 				$cret['sslPemKeyFile'] = DATA_PATH.'wechat/app_cret/apiclient_key.pem';
// 			break;
// 			case 'web':
// 			default:
// 				$cret['sslPemFile'] = DATA_PATH.'wechat/web_cret/apiclient_cert.pem';
// 				$cret['sslPemKeyFile'] = DATA_PATH.'wechat/web_cret/apiclient_key.pem';
// 			break;
// 		}
// 		return $cret;
// 	}
	
	
}

